package com.auto.ding

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.util.Log.i
import androidx.appcompat.app.AlertDialog
import com.auto.ding.service.MainAccessService
import com.ding.ding.AppKit
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*


class MainActivity : Activity() {
    private var nowIndex = 0
    private val timeShangList =
        arrayListOf(
            "8:09:09",
            "7:43:23",
            "8:14:05",
            "7:27:02",
            "8:13:05",
            "8:19:04",
            "8:08:07",
            "8:23:45",
            "8:13:05",
            "7:37:29",
            "8:19:03",
            "8:08:07",
            "8:26:03",
            "7:39:01"
        )
    @SuppressLint("SimpleDateFormat")
    val simpleDateFormat = SimpleDateFormat("HHmmss")

    private var handlerGoHome: Handler = Handler()

    private fun goHome() {
//        handlerGoHome.postDelayed({
//            i(" handlerGoHome -->", "  ")
//            val intent2 = Intent(Intent.ACTION_MAIN)
//        intent2.flags = Intent.FLAG_ACTIVITY_NEW_TASK
//        intent2.addCategory(Intent.CATEGORY_HOME)
//        startActivity(intent2)
//        }, 1000 * 10)

        handlerGoHome.postDelayed({
            i(" clickBackButton -->", "  ")
            AppKit.clickBackButton()
            AppKit.clickBackButton()
            AppKit.clickBackButton()
            AppKit.openApplicationByPackage("com.auto.ding", this)
            AppKit.clickBackButton()
            AppKit.clickBackButton()
            AppKit.clickBackButton()
        }, 1000 * 10)

    }

    private var handlerOpenDing: Handler = Handler()
    private fun openDing() {
        handlerOpenDing.postDelayed({
            val nowTime = simpleDateFormat.format(Date())

            val shangTimeInt = shang_1.text.toString().replace(":", "").toInt()


            //检查上班
            if (nowTime.toInt() in shangTimeInt..83000) {
                i("上班打卡-->", nowTime)
                AppKit.openApplicationByPackage("com.alibaba.android.rimet", this)
                goHome()
            } else {
                i("上班打卡 -->", "时间不到 ")
            }

            //检查下班
            if (nowTime.toInt() in 173000..173500) {
                i("下班打卡-->", nowTime)
                AppKit.openApplicationByPackage("com.alibaba.android.rimet", this)
                goHome()
            } else {
                i("下班打卡 -->", "时间不到 ")
            }

            i("nowTime -->", nowTime)
            i("上班开始时间-->", shangTimeInt.toString())
            i("下班开始时间-->", "173000")
            openDing()
        }, 1000 * 25)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        checkAccess()
        openDing()
        //加15
        huan.setOnClickListener {
            nowIndex += 1
            if (nowIndex > timeShangList.size - 1) nowIndex = 0
            shang_1.text = timeShangList[nowIndex]
        }
    }

    override fun onBackPressed() {

    }

    private fun checkAccess() {
        if (!AppKit.isSettingOpen(MainAccessService::class.java, this)) {
            val builder = AlertDialog.Builder(this)
            builder.setTitle("提示")
            builder.setMessage("请开启无障碍模式\n应用名称：DiDiDiDi")
            builder.setPositiveButton("点击前往设置",
                { dialogInterface, i ->
                    AppKit.jumpToSetting(this@MainActivity)
                })
            builder.create().show()
        }
    }


//    fun runRootCommand(command: String = "ROOT"): Boolean {
//        var process: Process? = null
//        var os: DataOutputStream? = null
//        try {
//            process = Runtime.getRuntime().exec("su")
//            os = DataOutputStream(process!!.outputStream)
//            os.writeBytes(command + "\n")
//            os.writeBytes("exit\n")
//            os.flush()
//            process.waitFor()
//        } catch (e: Exception) {
//            Log.d(TAG, "the device is not rooted,  error message： " + e.message)
//            return false
//        } finally {
//            try {
//                if (os != null) {
//                    os.close()
//                }
//                process?.destroy()
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//
//        }
//        return true
//    }


}